<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>18.1测试请求返回object</title>
  </head>
  <body>
    <div id="app">
      <table border="1" style="width: 300px; text-align: center">
        <thead>
          <th>id</th>
          <th>用户名</th>
          <th>密码</th>
        </thead>
        <tbody v-show="hasPerson">
          <tr v-for="item in person" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.username}}</td>
            <td>{{item.password}}</td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td colspan="3">
              <button @click="getObject">测试请求返回object</button>
            </td>
          </tr>
        </tfoot>
      </table>
      <div>
        <input
          type="text"
          placeholder="请输入用户名"
          v-model="username"
        /><br />
        <input type="text" placeholder="请输入密码" v-model="password" /> <br />
        <button @click="addOject()">添加数据</button>
      </div>
    </div>

    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          username: "",
          password: "",
          person:[],
          // person: {
          //   id: "",
          //   username: "",
          //   password: "",
          // },
        },
        methods: {
          async getObject() {
            const res = await axios({
              url: "http://127.0.0.1:8000/api/getObject",
              method: "get",
              params: {},
            });
            console.log(res.data.obj);
            this.person = res.data.obj;
          },
          //           async getObject(){
          //     const res = await axios({
          //         url: 'http://127.0.0.1:8000/api/getObject',
          //         method: "post",
          //         data: {}, // Use data instead of params for POST requests
          //     });
          //     console.log(res.data.obj);
          //     this.person = res.data.obj;
          // }
          async addOject() {
            if(this.username =='' || this.password ==''){
                alert('必填属性不可以为空');
                return;
            }
            console.log(this.username, this.password);
            await axios({
              url: "http://127.0.0.1:8000/api/addObject",
              method: "post",
              data: {
                username: this.username,
                password: this.password,
              },
            });
          },
        },
        computed: {
          hasPerson() {
            return (
              this.person &&
              this.person.id &&
              this.person.name &&
              this.person.password
            );
          },
        },
      });
    </script>
  </body>
</html>
